package org.dromara.lessor.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.lessor.domain.vo.HouseVo;
import org.dromara.lessor.domain.bo.HouseBo;
import org.dromara.lessor.service.IHouseService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 出租方房源信息
 *
 * @author Lion Li
 * @date 2025-09-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lessor/house")
public class LessorHouseController extends BaseController {

    private final IHouseService houseService;
    private final ISysOssService ossService;

    /**
     * 查询我的房源信息列表
     */
//    @SaCheckPermission("lessor:house:list")
    @GetMapping("/myList")
    public TableDataInfo<HouseVo> myList(HouseBo bo, PageQuery pageQuery) {
        bo.setLessorId(LoginHelper.getUserId());
        return houseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出房源信息列表
     */
//    @SaCheckPermission("lessor:house:export")
    @Log(title = "房源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HouseBo bo, HttpServletResponse response) {
        List<HouseVo> list = houseService.queryList(bo);
        ExcelUtil.exportExcel(list, "房源信息", HouseVo.class, response);
    }

    /**
     * 获取房源信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("lessor:house:query")
    @GetMapping("/{id}")
    public R<HouseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(houseService.queryById(id));
    }

    /**
     * 新增房源信息
     */
//    @SaCheckPermission("lessor:house:add")
    @Log(title = "房源信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HouseBo bo) {
        bo.setLessorId(LoginHelper.getUserId());
        return toAjax(houseService.insertByBo(bo));
    }

    /**
     * 修改房源信息
     */
//    @SaCheckPermission("lessor:house:edit")
    @Log(title = "房源信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HouseBo bo) {
        return toAjax(houseService.updateByBo(bo));
    }

    /**
     * 删除房源信息
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("lessor:house:remove")
    @Log(title = "房源信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(houseService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 上传OSS对象存储
     *
     * @param file 文件
     */
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOssUploadVo> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        SysOssVo oss = ossService.upload(file);
        SysOssUploadVo uploadVo = new SysOssUploadVo();
        uploadVo.setUrl(oss.getUrl());
        uploadVo.setFileName(oss.getOriginalName());
        uploadVo.setOssId(oss.getOssId().toString());
        return R.ok(uploadVo);
    }


}
